<template>
  <div class="app-container server-monitor">

    <LastUpdate :time="time" :onClickUpdate="getList" style="margin: 0 15px;"/>

    <el-row>
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span>CPU</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;" align="center">
              <thead>
                <tr>
                  <th>属性</th>
                  <th>值</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>核心数</td>
                  <td v-if="server.cpu">{{ server.cpu.cpuNum }}</td>
                </tr>
                <tr>
                  <td>用户使用率</td>
                  <td v-if="server.cpu">{{ server.cpu.used }}%</td>
                </tr>
                <tr>
                  <td>系统使用率</td>
                  <td v-if="server.cpu">{{ server.cpu.sys }}%</td>
                </tr>
                <tr>
                  <td>当前空闲率</td>
                  <td v-if="server.cpu">{{ server.cpu.free }}%</td>
                </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span>内存</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
                <tr>
                  <th>属性</th>
                  <th>内存</th>
                  <th>JVM</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>总内存</td>
                  <td v-if="server.mem">{{ server.mem.total }}G</td>
                  <td v-if="server.jvm">{{ server.jvm.total }}M</td>
                </tr>
                <tr>
                  <td>已用内存</td>
                  <td v-if="server.mem">{{ server.mem.used}}G</td>
                  <td v-if="server.jvm">{{ server.jvm.used}}M</td>
                </tr>
                <tr>
                  <td>剩余内存</td>
                  <td v-if="server.mem">{{ server.mem.free }}G</td>
                  <td v-if="server.jvm">{{ server.jvm.free }}M</td>
                </tr>
                <tr>
                  <td>使用率</td>
                  <td v-if="server.mem" :class="{'text-danger': server.mem.usage > 80}">{{ server.mem.usage }}%</td>
                  <td v-if="server.jvm" :class="{'text-danger': server.jvm.usage > 80}">{{ server.jvm.usage }}%</td>
                </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

      <el-col :span="24" class="card-box">
        <el-card>
          <div slot="header">
            <span>磁盘状态</span>
          </div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
                <tr>
                  <th>盘符路径</th>
                  <th>文件系统</th>
                  <th>盘符类型</th>
                  <th>总大小</th>
                  <th>可用大小</th>
                  <th>已用大小</th>
                  <th>已用百分比</th>
                </tr>
              </thead>
              <tbody v-if="server.sysFiles">
                <tr v-for="sysFile in server.sysFiles">
                  <td>{{ sysFile.dirName }}</td>
                  <td>{{ sysFile.sysTypeName }}</td>
                  <td>{{ sysFile.typeName }}</td>
                  <td>{{ sysFile.total }}</td>
                  <td>{{ sysFile.free }}</td>
                  <td>{{ sysFile.used }}</td>
                  <td :class="{'text-danger': sysFile.usage > 80}">{{ sysFile.usage }}%</td>
                </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

      <el-col :span="24" class="card-box">
        <el-card>
          <div slot="header">
            <span>服务器信息</span>
          </div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <tbody>
                <tr>
                  <td>服务器名称</td>
                  <td v-if="server.sys">{{ server.sys.computerName }}</td>
                  <td>操作系统</td>
                  <td v-if="server.sys">{{ server.sys.osName }}</td>
                </tr>
                <tr>
                  <td>服务器IP</td>
                  <td v-if="server.sys">{{ server.sys.computerIp }}</td>
                  <td>系统架构</td>
                  <td v-if="server.sys">{{ server.sys.osArch }}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

      <el-col :span="24" class="card-box">
        <el-card>
          <div slot="header">
            <span>Java虚拟机信息</span>
          </div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <tbody>
                <tr>
                  <td>Java名称</td>
                  <td v-if="server.jvm">{{ server.jvm.name }}</td>
                  <td>Java版本</td>
                  <td v-if="server.jvm">{{ server.jvm.version }}</td>
                </tr>
                <tr>
                  <td>启动时间</td>
                  <td v-if="server.jvm">{{ server.jvm.startTime }}</td>
                  <td>运行时长</td>
                  <td v-if="server.jvm">{{ server.jvm.runTime }}</td>
                </tr>
                <tr>
                  <td colspan="1">安装路径</td>
                  <td colspan="3"><div v-if="server.jvm">{{ server.jvm.home }}</div></td>
                </tr>
                <tr>
                  <td colspan="1">项目路径</td>
                  <td colspan="3"><div v-if="server.sys">{{ server.sys.userDir }}</div></td>
                </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

    </el-row>
  </div>
</template>

<style scoped>
  table th{
    text-align: left;
    padding-left: 10px;
    padding-right: 10px;
    font-size: 16px;
    font-weight: bold;
    color: black;
  }
  table td{
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-break: break-all;
    line-height: 23px;
    padding-left: 10px;
    padding-right: 10px;
  }
</style>

<script>
import {httpGet} from '@/api';

export default {
  name: "Server",
  data() {
    return {
      // 加载层信息
      loading: [],
      // 服务器信息
      server: [],
      time: '',
      url:{
        getInfo:"/monitor/server/info"
      }
    };
  },
  created() {
  },
  mounted(){
    this.getList();
  },
  methods: {
    /** 查询服务器信息 */
    getList() {
      this.openLoading();
      httpGet(this.url.getInfo).then(response => {
        this.time = this.$formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
        this.server = response.data;
        this.loading.close();
      });
    },
    // 打开加载层
    openLoading() {
      this.loading = this.$loading({
        target: '.server-monitor',
        lock: true,
        text: "拼命读取中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    }
  }
};
</script>
